package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-gate/cache"
	"novel-gate/model"
	"novel-gate/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username = ?", username).Find(&user).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}

	redis_key := "login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	if user.Password != pkg.Md5str1(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "登入失败",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登入失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登入成功",
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var novel []model.Novel
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Offset(offset).Limit(size).Find(&novel)

		jsonData, _ := json.Marshal(&novel)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*3)

	} else {

		json.Unmarshal([]byte(cacheData), &novel)

	}
	var total int64
	model.DB.Model(&model.Novel{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"count": total,
		"data":  novel,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var novel model.Novel
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Where("id = ?", id).Find(&novel)

		jsonData, _ := json.Marshal(&novel)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*3)
	} else {
		json.Unmarshal([]byte(cacheData), &cacheData)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": novel,
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")
	redis_key := "del_" + id
	cache.Rdb.Del(cache.Ctx, redis_key)
	model.DB.Where("id = ?", id).Delete(&model.Novel{})
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
func Add(c *gin.Context) {
	title := c.PostForm("title")
	comment := c.PostForm("comment")
	author := c.PostForm("author")
	if title == "" || comment == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Novel{
		Title:   title,
		Comment: comment,
		Author:  author,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
